package com.lizard.schedule.ui.adapter.schedule;

import com.lizard.schedule.persistence.db.bean.Schedule;

import java.util.Comparator;

/**
 * 用于ScheduleListAdapter中的date集合排序比较器
 */
public class ScheduleComparetor implements Comparator<ScheduleFacade> {

    /**
     * 排序方式
     */
    private int mSortBy;

    /**
     *
     * @param sortBy ScheduleListAdapter中的排序方式
     */
    public ScheduleComparetor(int sortBy){
        mSortBy = sortBy;
    }

    @Override
    public int compare(ScheduleFacade scheduleFacade1, ScheduleFacade scheduleFacade2) {
        if(scheduleFacade1 == null
                || scheduleFacade1.getSchedule() == null
                || scheduleFacade2 == null
                || scheduleFacade2.getSchedule() == null){
            return 0;
        }

        if(mSortBy == ScheduleListAdapter.SORT_BY_FUTURE){
            if(scheduleFacade1.getSchedule().getBeginTime() == scheduleFacade2.getSchedule().getBeginTime()){
                if(scheduleFacade1.getSchedule().getLocalId() < scheduleFacade2.getSchedule().getLocalId()){ // localId从小到大排序
                    return -1;
                } else {
                    return 1;
                }
            }

            if(scheduleFacade1.getSchedule().getBeginTime() < scheduleFacade2.getSchedule().getBeginTime()){ // 时间从小到大排序
                return -1;
            } else {
                return 1;
            }
        } else{ // 历史计划
            if(scheduleFacade1.getSchedule().getStatus() != scheduleFacade2.getSchedule().getStatus()){ // 状态不一样的计划中，“未完成”的靠前
                if(scheduleFacade1.getSchedule().getStatus() == Schedule.STATUS_UNFINISHED){
                    return -1;
                } else{
                    return 1;
                }
            } else{
                if(scheduleFacade1.getSchedule().getBeginTime() == scheduleFacade2.getSchedule().getBeginTime()){
                    if(scheduleFacade1.getSchedule().getLocalId() < scheduleFacade2.getSchedule().getLocalId()){ // localId从大到小排序
                        return 1;
                    } else {
                        return -1;
                    }
                }

                if(scheduleFacade1.getSchedule().getBeginTime() < scheduleFacade2.getSchedule().getBeginTime()){ // 时间从大到小排序
                    return 1;
                } else {
                    return -1;
                }
            }
        }
    }
}
